// 1)Promise类  承诺 允诺 （promise有三个状态） 等待态 pending 成功态 fufilled 失败态 rejected
// 2)executor 会立即执行 把resolve,reject 也是一个函数
// 3)每一个promise 上都有一个then方法 then方法中传递两个函数 分别代表成功后执行的回调和失败后执行的回调
// 4)一旦成功 就不能在失败 一旦失败就不能在成功
function Promise(executor) {
  let that = this;
  that.status = 'pending';
  that.value = undefined;
  that.reason = undefined;
  function resolve(value) { // 调用了resolve 当前状态就变成了成功态
    if(that.status === 'pending'){
      that.status = 'fulfilled';
      that.value = value;
    }
  }
  function reject(reason) { // 调用reject 就变成了失败态
    if(that.status === 'pending'){
      that.status = 'rejected';
      that.reason = reason;
    }
  }
  executor(resolve,reject);
}
Promise.prototype.then = function (onFulfiled,onRejected) {
  let that = this;
  if (that.status === 'fulfilled'){
    onFulfiled(that.value);
  }
  if(that.status === 'rejected'){
    onRejected(that.reason);
  }
}
module.exports = Promise;